package org.db2.project.airline.dao.impl;

import org.db2.project.airline.dao.AirplaneDAO;
import org.db2.project.airline.dao.DAOException;
import org.db2.project.airline.model.Airplane;
import org.db2.project.airline.model.Flight;
import org.db2.project.airline.model.FlightPlan;
import org.hibernate.HibernateException;

/**
 *
 */
public class HibernateAirplaneDAO extends HibernateGenericDAO<Airplane> implements AirplaneDAO {

    public HibernateAirplaneDAO() {
        // empty constructor
    }

    @Override
    public Airplane findAirplane(FlightPlan flightPlan) throws DAOException {
        try {
            return (Airplane) getSession().createQuery(
                    "select a from FlightPlan fp join fp.flight.airplane a where fp.id = :flightPlanId")
                    .setParameter("flightPlanId", flightPlan.getId())
                    .uniqueResult();
        } catch (HibernateException e) {
            throw new DAOException("Error occurred when retrieving entity.", e);
        }
    }
}
